//
//  ContentView.swift
//  AddShippingAddress
//
//  Created by hqcMac on 2020/4/15.
//  Copyright © 2020 hqc. All rights reserved.
//

import SwiftUI

struct ContentView: View {
    @State private var name = ""
    @State private var phone = ""
    @State private var address = ""
    @State private var tagIndex = 0
    @State private var showAlert = false
    let tags = ["家", "学校", "公司"]
    var confirMsg: String{
        """
        收货人：\(name)
        手机：\(phone)
        详细地址：\(address)
        标签：\(tags[self.tagIndex])
        """
    }
    var invalidInput: Bool{
        name.isEmpty || phone.isEmpty || address.isEmpty
    }
    
    var body: some View {
        NavigationView{
            Form{
                Section{
                    HStack {
                        Text("收货人")
                        TextField("请填写收货人姓名", text: $name)
                    }
                    HStack {
                        Text("手机")
                        TextField("请填写收货人手机", text: $phone)
                            .keyboardType(.numberPad)
                    }
                    HStack {
                        Text("详细地址")
                        TextField("请填写收货人详细地址", text: $address)
                    }
                }
                Section(header: Text("标签") ){
                    Picker("标签", selection: $tagIndex) {
                        ForEach(0..<tags.count, id: \.self) { index in
                            Text(self.tags[index])
                        }
                        
                    }
                    .pickerStyle(SegmentedPickerStyle())
                }
                Section{
                    Button("填好了"){
                        self.showAlert = true
                    }
                    .disabled(invalidInput)
                    
                }
            }
            .navigationBarTitle("新增收货地址")
            .alert(isPresented: $showAlert) {
                Alert(title: Text("确认信息"), message: Text(confirMsg), dismissButton: .default(Text("确认了")))
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
